/*
 * stm32f10x_adf.cpp
 *
 *  Created on: 01.06.2012
 *      Author: Roma Jam
 */

#include "stm32f10x.h"
#include "stm32f10x_adf.h"
#include "adf.h"
#include "kl_lib.h"

ADFSetup_t AdfChip;
void ADFSetup_t::Setup(int FRAC_LIT, AdfDescriptions_t *PAdf) {
//**********************ADF_4350_Struct_Initialization**************************
    ADF_InitTypeDef ADF_InitStructure;
// REGISTER0
    if (PAdf == Adf1424Mhz) {                                                   // For 1424MHz
        ADF_InitStructure.ADF_INT = 142;                                        // Initeger value of divider - INT
    }
    else if (PAdf == Adf248MHz) {                                                    // For 248MHz
        ADF_InitStructure.ADF_INT = 198;                                        // Initeger value of divider - INT                              // Fractional value of divider - FRAC
    }
    else { CmdUnit.Printf("ERROR00\r"); return;}
    ADF_InitStructure.ADF_FRAC = FRAC_LIT + 8;                                  // Fractional value of divider - FRAC
// REGISTER1
    if (PAdf == Adf1424Mhz) {
        ADF_InitStructure.ADF_Prescaler = Dual_modulus_prescaler4_5;                // Modulus of prescaler
    }
    else if (PAdf == Adf248MHz) {
        ADF_InitStructure.ADF_Prescaler = Dual_modulus_prescaler8_9;                // Modulus of prescaler
    }
    ADF_InitStructure.ADF_PHASE = 18;                                           // Phase value  - PHASE
    ADF_InitStructure.ADF_MOD = 20;                                             // Modulus value - MOD
// REGISTER2
    ADF_InitStructure.ADF_NoiseMode = NoiseMode_LowNoiseMode;                   // Noise mode
    ADF_InitStructure.ADF_Muxout = Muxout_AnalogLockDetect;                     // Muxout
    ADF_InitStructure.ADF_RefDoubler = RefDoubler_Enabled;                      // Reference doubler buffer
    ADF_InitStructure.ADF_RefDivider = RefDivider_Enabled;                      // Reference divider by 2
    ADF_InitStructure.ADF_R_COUNTER = 1;                                        // R COUNTER
    ADF_InitStructure.ADF_DoubleBuffer = DoubleBuffer_Enabled;                  // Enable double Buffer bits in REGISTER4
    ADF_InitStructure.ADF_ChargePumpCurrent = ChargePumpCurrent_5000uA;         // Charge pump current
    ADF_InitStructure.ADF_LDF = LDF_FRAC_N;                                     // FRAC Lock detect
    ADF_InitStructure.ADF_LDP = LDP_10ns;                                       // Precision Lock detect
    ADF_InitStructure.ADF_PdPolarity = PdPolarity_Positive;                     // PD polarity
    ADF_InitStructure.ADF_PowerDown = PowerDown_Disabled;                       // Power down mode
    ADF_InitStructure.ADF_CpThreeState = CpThreeState_Disabled;                 // CP three state
    ADF_InitStructure.ADF_CounterReset = CounterReset_Disabled;                 // Counter reset
// REGISTER3
    ADF_InitStructure.ADF_CycleSlipReduction = CycleSlipReduction_Disabled;     // Cycle slip reduction
    ADF_InitStructure.ADF_ClockDividerMode = ClockDividerMode_FastLockEnable;   // Clock divider mode
    ADF_InitStructure.ADF_CLOCK_DIV = 400;                                      // Clock divider value
// REGISTER4
    ADF_InitStructure.ADF_FeedbackSelect = FeedbackSelect_Fundamental;          // Feedback select
    if (PAdf == Adf1424Mhz) {
        ADF_InitStructure.ADF_RfDivider = RfDivider_2;                              // RF divider
    }
    else if (PAdf == Adf248MHz) {
        ADF_InitStructure.ADF_RfDivider = RfDivider_16;                              // RF divider
    }
    else { CmdUnit.Printf("ERROR01\r"); return;}
    ADF_InitStructure.ADF_BAND_CLOCK_DIV = 253;                                 // Band clock divider value
    ADF_InitStructure.ADF_VcoPowerMode = VcoPowerMode_PoweredUp;                // VCO powered up
    ADF_InitStructure.ADF_Mute = Mute_Enabled;                                  // Mute select
    ADF_InitStructure.ADF_AuxOutputSelect = AuxOutputSelect_DividedOutput;      // AUX output select
    ADF_InitStructure.ADF_AuxEnabler = AuxEnabler_Disabled;                     // Enable/disable AUX output
    ADF_InitStructure.ADF_AuxOutputPower = AuxOutputPower_Dec4;                 // AUX output power
    ADF_InitStructure.ADF_RfOutput = RfOutput_Enabled;                          // RF output
    ADF_InitStructure.ADF_OutputPower = OutputPower_Dec4;                       // Output power choose
// REGISTER5
    ADF_InitStructure.ADF_LockDetectPinMode = LockDetectPinMode_High;           // Lock detect pin mode

//**********************Forming_Registers_Sequence******************************
// Form REGISTER0_SEQ
    REGISTER0_SEQ = 0;
    REGISTER0_SEQ <<= 16;
    REGISTER0_SEQ |= ADF_InitStructure.ADF_INT;
    REGISTER0_SEQ <<= 12;
    REGISTER0_SEQ |= ADF_InitStructure.ADF_FRAC;
    REGISTER0_SEQ <<= 3;
    REGISTER0_SEQ |= REGISTER0_ADDR;

// Form REGISTER1_SEQ
    REGISTER1_SEQ = 0;
    REGISTER1_SEQ <<= 4;
    REGISTER1_SEQ |= ADF_InitStructure.ADF_Prescaler;
    REGISTER1_SEQ <<= 12;
    REGISTER1_SEQ |= ADF_InitStructure.ADF_PHASE;
    REGISTER1_SEQ <<= 12;
    REGISTER1_SEQ |= ADF_InitStructure.ADF_MOD;
    REGISTER1_SEQ <<= 3;
    REGISTER1_SEQ |= REGISTER1_ADDR;

// Form REGISTER2_SEQ
    REGISTER2_SEQ = 0;
    REGISTER2_SEQ <<= 2;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_NoiseMode;
    REGISTER2_SEQ <<= 3;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_Muxout;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_RefDoubler;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_RefDivider;
    REGISTER2_SEQ <<= 10;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_R_COUNTER;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_DoubleBuffer;
    REGISTER2_SEQ <<= 4;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_ChargePumpCurrent;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_LDF;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_LDP;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_PdPolarity;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_PowerDown;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_CpThreeState;
    REGISTER2_SEQ <<= 1;
    REGISTER2_SEQ |= ADF_InitStructure.ADF_CounterReset;
    REGISTER2_SEQ <<= 3;
    REGISTER3_SEQ |= REGISTER2_ADDR;

// Form REGISTER3_SEQ
    REGISTER3_SEQ = 0;
    REGISTER3_SEQ <<= 13;
    REGISTER3_SEQ |= ADF_InitStructure.ADF_CycleSlipReduction;
    REGISTER3_SEQ <<= 3;
    REGISTER3_SEQ |= ADF_InitStructure.ADF_ClockDividerMode;
    REGISTER3_SEQ <<= 12;
    REGISTER3_SEQ |= ADF_InitStructure.ADF_CLOCK_DIV;
    REGISTER3_SEQ <<= 3;
    REGISTER3_SEQ |= REGISTER3_ADDR;

// Form REGISTER4_SEQ
    REGISTER4_SEQ = 0;
    REGISTER4_SEQ <<= 8;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_FeedbackSelect;
    REGISTER4_SEQ <<= 3;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_RfDivider;
    REGISTER4_SEQ <<= 8;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_BAND_CLOCK_DIV;
    REGISTER4_SEQ <<= 1;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_VcoPowerMode;
    REGISTER4_SEQ <<= 1;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_Mute;
    REGISTER4_SEQ <<= 1;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_AuxOutputSelect;
    REGISTER4_SEQ <<= 1;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_AuxEnabler;
    REGISTER4_SEQ <<= 2;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_AuxOutputPower;
    REGISTER4_SEQ <<= 1;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_RfOutput;
    REGISTER4_SEQ <<= 2;
    REGISTER4_SEQ |= ADF_InitStructure.ADF_OutputPower;
    REGISTER4_SEQ <<= 3;
    REGISTER4_SEQ |= REGISTER4_ADDR;

// Form REGISTER5_SEQ
    REGISTER5_SEQ = 0;
    REGISTER5_SEQ <<= 9;
    REGISTER5_SEQ |= ADF_InitStructure.ADF_LockDetectPinMode;
    REGISTER5_SEQ <<= 3;
    REGISTER5_SEQ |= 2;
    REGISTER5_SEQ <<= 19;
    REGISTER5_SEQ |= REGISTER5_ADDR;

// Load to chip
    Adf.WriteToRegister(PAdf, REGISTER5_SEQ);
    Adf.WriteToRegister(PAdf, REGISTER4_SEQ);
    Adf.WriteToRegister(PAdf, REGISTER3_SEQ);
    Adf.WriteToRegister(PAdf, REGISTER2_SEQ);
    Adf.WriteToRegister(PAdf, REGISTER1_SEQ);
    Adf.WriteToRegister(PAdf, REGISTER0_SEQ);

}
